﻿using Microsoft.EntityFrameworkCore;
using Naruto.Repository;
using Naruto.TaskManagement.Entitys;
using Naruto.TaskManagement.Entitys.Enums;
using Naruto.TaskManagement.Entitys.Models;
using Naruto.TaskManagement.Entitys.VM;
using Naruto.TaskManagement.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Naruto.TaskManagement.Repository
{
    public class OrganizationInviteRecordRepository : IOrganizationInviteRecordRepository
    {
        private readonly IRepository<TaskManagementContext> repository;

        public OrganizationInviteRecordRepository(IRepository<TaskManagementContext> _repository)
        {
            repository = _repository;
        }

        public async Task Add(OrganizationInviteRecord info)
        {
            await repository.Command<OrganizationInviteRecord>().AddAsync(info);
        }

        /// <summary>
        /// 根据id 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<GetByIdViewModel> GetById(string id)
        {
            var query = await repository.QueryAsync<OrganizationInviteRecord>();
            return await query.Where(a => a.Id == id).AsNoTracking()
                   .Select<OrganizationInviteRecord, GetByIdViewModel>().FirstOrDefaultAsync();
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task UpdateStatusById(string id, OrganizationInviteRecordStatusEnum statusEnum)
        {
            await repository.Command<OrganizationInviteRecord>()
                 .UpdateAsync(a => a.Id == id, entity =>
                 {
                     entity.Status = (int)statusEnum;
                     entity.OperationTime = DateTime.Now;
                     return entity;
                 });
        }
    }
}
